package com.fjl.basic;

public class StopThreadTest
{

	public static void main(String[] args)
	{
		StopThread st = new StopThread();

		Thread t1 = new Thread(st);
		Thread t2 = new Thread(st);

		t1.setDaemon(true); /* 守护线程，也叫后台线程，主线程一挂，后台线程也跟着挂 */
		t2.setDaemon(true);
		t1.start();
		t2.start();

		int num = 0;

		while (true)
		{
			if (num++ == 60)
			{
				// st.changeFlag();
				t1.interrupt();
				// t2.interrupt();
				break;
			}
			System.out.println(Thread.currentThread().getName() + "......." + num);
		}
		System.out.println("over");
	}

	// 线程
	public static void threadSleep(Thread thread,long time)
	{
		try
		{
			Thread.sleep(time);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}

	static class StopThread implements Runnable
	{
		private boolean flag = true;

		public void run()
		{
			while (flag)
			{

				System.out.println(Thread.currentThread().getName() + "....run");
			}
		}

		public void changeFlag()
		{
			flag = false;
		}
	}

}
